CC = gcc
CFLAGS = -g -O0 -W  -Wall

COMMON_PATH = ../common
INC = -I . -I $(COMMON_PATH)

KERNEL_PATH = /root/linux-5.10.100
ROOTFS_PATH = /root/linux-5.10.100/kernel-busybox/busybox-1.32.0
SHAREFS_PATH = /root/linux-5.10.100/kernel-busybox/shareDisk

SRCS = $(wildcard ./*.c) $(wildcard $(COMMON_PATH)/*.c)
OBJS = $(patsubst %.c, %.o, $(SRCS))

CURRENT_DIR = $(notdir $(shell pwd))
TARGET = $(CURRENT_DIR)

.PHONY: clean

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $^ -o $@
#----------------------------------------------------------------------
#注意我们的程序必须被静态链接，并且程序名必须为init
#通过cpio，将init程序直接打包进rootfs.img.ourprogram
ourprogram-net-debug:
	$(CC) $(CFLAGS) $(INC) $(SRCS) -o init -static -lpthread
	find init | cpio -o -Hnewc | gzip -9 > rootfs.img.ourprogram
	qemu-system-x86_64 \
		-kernel $(KERNEL_PATH)/arch/x86_64/boot/bzImage \
		-hda $(ROOTFS_PATH)/rootfs.img \
		-hdb $(SHAREFS_PATH)/ext4.img \
		-display none -serial mon:stdio \
        -append "root=/dev/sda console=ttyS0 sched_debug loglevel=8 nokaslr" \
        -D ./qemu_log_`date +%Y%m%d%H%M`.txt \
		-nographic \
		-S -s \
		-net nic -net tap,ifname=tap0,script=no,downscript=no 

# -initrd rootfs.img.ourprogram \
# -smp 1 \
# -smp cpus=2,cores=2,threads=1 \
# -m 1024 \
# -hda $(ROOTFS_PATH)/rootfs.img \
# -net nic -net tap,ifname=tap0,script=no,downscript=no 
#----------------------------------------------------------------------
# 在qemu里新建网卡
# ifconfig eth0 192.168.0.168 netmask 255.255.255.0 
# ifconfig 

# 虚拟机里启动tun/tap
# sudo ifconfig tap0 192.168.0.169
# 虚拟机连接 qemu中内核中的网络
# nc 192.168.0.168 9090
#----------------------------------------------------------------------
gdb:
	gdb ${KERNEL_PATH}/vmlinux
#----------------------------------------------------------------------

#qemu卡死时
# ps -ef | grep qemu | awk '{print $2}' | xargs kill
#----------------------------------------------------------------------
%.o: %.c
	$(CC) $(CFLAGS) $(INC) -c $< -o $@

clean:
	rm -f $(OBJS) $(TARGET) init rootfs.img.ourprogram ./qemu_log*
#----------------------------------------------------------------------
